import { Controller } from "../../../../mvc/core/Controller";
import { WindowEvent } from "../../../../component/window/WindowEvent";
import { GameProxy } from "../../../mvc/GameProxy";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { DataEvent } from "../../../events/DataEvent";
import { GameController } from "../../../mvc/GameController";
import { SIslandPos } from "../../../../../Message/Game/SIslandPos";
import { PetIslandMapPanel } from "../PetIslandMapPanel";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SIslandInfo } from "../../../../../Message/Game/SIslandInfo";
import { SPlaceInfo } from "../../../../../Message/Game/SPlaceInfo";
import { SPlaceMiniInfo } from "../../../../../Message/Game/SPlaceMiniInfo";
import { SRobIslandGuildFightingInfo } from "../../../../../Message/Game/SRobIslandGuildFightingInfo";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { PetIslandMapItemTip } from "../PetIslandMapItemTip";
import { PetIslandMapDiscardPanel } from "../PetIslandMapDiscardPanel";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { IView } from "../../../../mvc/interfaces/IView";
type int = number;
//class PetIslandMapController
    
    export  class PetIslandMapController extends Controller
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */showPanel(arg1: WindowEvent): void
        {
            GameProxy.petIsland.getGuildInfo();
            GameProxy.petIsland.getPlayerInfo();
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandPlaceInfo, this.petIslandPlaceInfo);
            return;
        }

        /* internal  */closePanel(arg1: WindowEvent): void
        {
            this.clean();
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicRobIslandPlaceInfo, this.petIslandPlaceInfo);
            return;
        }

        /* internal  */onPetIslandMapShowHandler(arg1: DataEvent): void
        {
            GameController.petIsland.powerBaseController.view.hide();
            view.show();
            return;
        }

        /* internal  */onPetIslandMapShowByPositionHandler(arg1: DataEvent): void
        {
            this._pointData = arg1.data as SIslandPos;
            this._isClickCoordinatePoint = true;
            GameProxy.petIsland.getIslandInfo(this._pointData.isLand);
            return;
        }

        /* internal  */onPetIslandSetMapItemTipPositionHandler(arg1: DataEvent): void
        {
            PetIslandMapPanel(view).mapItemTipPosition = this.mapItemTip;
            return;
        }

        /* internal  */petIslandInfo(arg1: MessageBlock): void
        {
            var loc1=null;
            GameController.petIsland.powerBaseController.view.hide();
            if (this._isClickCoordinatePoint && view.isHide) 
            {
                view.show();
            }
            if (_view && !view.isHide) 
            {
                loc1 = arg1.messageBase as SIslandInfo;
                PetIslandMapPanel(view).setSmallMapData(loc1.places, this._isClickCoordinatePoint, this._pointData);
                this._isClickCoordinatePoint = false;
            }
            return;
        }

        /* internal  */petIslandPlaceInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlaceInfo;
            var loc2=new SPlaceMiniInfo();
            loc2.placeId = loc1.placeId;
            loc2.pos = loc1.pos;
            loc2.guildId = loc1.guildId;
            loc2.guildName = loc1.guildName;
            loc2.isFighting = loc1.isFighting;
            loc2.protectSeconds = loc1.protectSeconds;
            PetIslandMapPanel(view).updateItem(loc2);
            if (cache.petIsland.curPos && cache.petIsland.curPos.placeId == loc1.placeId || this.mapItemTip.data && this.mapItemTip.data.placeId == loc1.placeId) 
            {
                this.mapItemTip.data = loc1;
            }
            return;
        }

        /* internal  */onPetIslandInfoChangeHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobIslandGuildFightingInfo;
            if (loc1.fightings.length > 0) 
            {
                PetIslandMapPanel(view).infoChange();
            }
            return;
        }

        /* internal  */onPetIslandGarrisonHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.petIsland.movePet(loc1);
            return;
        }

        /* internal  */onPetIslandDiscardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.petIsland.dropPlace(loc1);
            return;
        }

        /* internal  */onPetIslandMapGetIslandInfoHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.petIsland.getIslandInfo(loc1);
            return;
        }

        /* internal  */onPetIslandMapGetPlaceInfoHandler(arg1: DataEvent): void
        {
            DisplayUtil.removeMe(this.mapItemTip);
            var loc1=arg1.data as SPlaceMiniInfo;
            cache.petIsland.curPos = loc1;
            GameProxy.petIsland.getPlaceInfo(loc1.placeId);
            return;
        }

        /* internal  */onPetIslandMapAttackHandler(arg1: DataEvent): void
        {
            var loc1=null;
            cache.petIsland.attackItem = arg1.data as SPlaceInfo;
            if (cache.petIsland.powerBaseInfo && cache.petIsland.powerBaseInfo.places.length >= 5) 
            {
                loc1 = cache.petIsland.powerBaseInfo.places;
                loc1.unshift(cache.petIsland.attackItem);
                if (this.discardPanel.isHide) 
                {
                    this.discardPanel.show();
                }
                this.discardPanel.data = loc1;
            }
            else 
            {
                GameProxy.petIsland.attackIsland(cache.petIsland.attackItem.placeId);
            }
            return;
        }

        /* internal  */onPetIslandMapDiscardItemSelectHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.petIsland.attackIsland(cache.petIsland.attackItem.placeId, loc1);
            return;
        }

        /* internal  */clean(): void
        {
            if (this._mapItemTip && this._mapItemTip.parent) 
            {
                DisplayUtil.removeMe(this.mapItemTip);
            }
            if (this._discardPanel && !this._discardPanel.isHide) 
            {
                this.discardPanel.hide();
            }
            return;
        }

        public get mapItemTip(): PetIslandMapItemTip
        {
            if (!this._mapItemTip) 
            {
                this._mapItemTip = new PetIslandMapItemTip();
            }
            return this._mapItemTip;
        }

        public get discardPanel(): PetIslandMapDiscardPanel
        {
            if (!this._discardPanel) 
            {
                this._discardPanel = new PetIslandMapDiscardPanel();
            }
            return this._discardPanel;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandIslandInfo, this.petIslandInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandGuildFightingInfo, this.onPetIslandInfoChangeHandler);
            Dispatcher.addEventListener(EventName.PetIslandGarrison, this.onPetIslandGarrisonHandler);
            Dispatcher.addEventListener(EventName.PetIslandDiscard, this.onPetIslandDiscardHandler);
            Dispatcher.addEventListener(EventName.PetIslandMapGetIslandInfo, this.onPetIslandMapGetIslandInfoHandler);
            Dispatcher.addEventListener(EventName.PetIslandMapGetPlaceInfo, this.onPetIslandMapGetPlaceInfoHandler);
            Dispatcher.addEventListener(EventName.PetIslandMapAttack, this.onPetIslandMapAttackHandler);
            Dispatcher.addEventListener(EventName.PetIslandMapDiscardItemSelect, this.onPetIslandMapDiscardItemSelectHandler);
            Dispatcher.addEventListener(EventName.PetIslandMapShow, this.onPetIslandMapShowHandler);
            Dispatcher.addEventListener(EventName.PetIslandMapShowByPosition, this.onPetIslandMapShowByPositionHandler);
            Dispatcher.addEventListener(EventName.PetIslandSetMapItemTipPosition, this.onPetIslandSetMapItemTipPositionHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this.petIslandMapPanel) 
            {
                this.petIslandMapPanel = new PetIslandMapPanel();
                this.petIslandMapPanel.addEventListener(WindowEvent.SHOW, this.showPanel);
                this.petIslandMapPanel.addEventListener(WindowEvent.CLOSE, this.closePanel);
            }
            return this.petIslandMapPanel;
        }

        public /* var */petIslandMapPanel: PetIslandMapPanel;

        /* internal */ /* var */_mapItemTip: PetIslandMapItemTip;

        /* internal */ /* var */_discardPanel: PetIslandMapDiscardPanel;

        /* internal */ /* var */_isClickCoordinatePoint: Boolean;

        /* internal */ /* var */_pointData: SIslandPos;
    }
